package hp.com.servlet.ProductServlet;

import hp.com.entity.PageInfo;
import hp.com.entity.Sells;
import hp.com.entity.Users;
import hp.com.service.ProductService;
import hp.com.service.impl.ProductServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/Types")
public class SellTypeServlet extends HttpServlet {
    private ProductService productService = new ProductServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //通过访问的session来获取用户对象
        HttpSession session = req.getSession();
        Users user =(Users) session.getAttribute("userSession");
        req.setAttribute("userSession",user);

        try {
            Object par = req.getParameter("pageNo");
            if (par==null){
                int category = Integer.parseInt(req.getParameter("category"));
                PageInfo<Sells> page = new PageInfo<>(1,8);
                page = productService.listPageByGroy(page,category);
                req.setAttribute("page",page);
                req.setAttribute("cate",category);
            }else {
                Object c =req.getParameter("category");
                int category = Integer.parseInt(req.getParameter("category"));
                int pageNo = Integer.parseInt(req.getParameter("pageNo"));
                PageInfo<Sells> page = new PageInfo<>(pageNo,8);
                page = productService.listPageByGroy(page,category);
                req.setAttribute("page",page);
                req.setAttribute("cate",category);
            }
            req.setAttribute("type","category");
            req.getRequestDispatcher(req.getContextPath()+"ShopPage/shop.jsp").forward(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doGet(req,resp);
    }
}
